<?php
namespace lib\http;
class HttpCurl
{
    public static function GetCurl($url,$time=5)
    {
        $curl=curl_init();
        curl_setopt($curl,CURLOPT_URL,$url);
        curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);//不检查ip证书是否有效
        curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,false);    //不检查域名证书
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);//设置是否有返回值
        curl_setopt($curl,CURLOPT_HEADER,false);
        curl_setopt($curl, CURLOPT_TIMEOUT, (int)$time);
        $rtl=curl_exec($curl);
        curl_close($curl);
        return $rtl;
    }
    public static function PostCurl($url,$data,$bool=false,$time=5)
    {
        if($bool)
        {
            $data=json_encode($data);
        }
        $curl=curl_init();
        curl_setopt($curl,CURLOPT_URL,(string)$url);
        curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);//不检查ip证书是否有效
        curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,false);    //不检查域名证书
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);//设置是否有返回值
        curl_setopt($curl,CURLOPT_HEADER,false);
        curl_setopt($curl, CURLOPT_TIMEOUT, $time);
        curl_setopt($curl, CURLOPT_POST, true);     //设置为post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        $rtl=curl_exec($curl);
        curl_close($curl);
        return $rtl;
    }
    public static function PostFile($url,$data,$time=5)
    {
        foreach ($data as $key=> $v)
        {
            if (is_file($v)&&file_exists($v))
            {
                $data[$key]=new CURLFile($v);
            }
        }
        $curl=curl_init();
        curl_setopt($curl,CURLOPT_URL,(string)$url);
        curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);//不检查ip证书是否有效
        curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,false);    //不检查域名证书
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);//设置是否有返回值
        curl_setopt($curl,CURLOPT_HEADER,false);
        curl_setopt($curl, CURLOPT_TIMEOUT, $time);
        curl_setopt($curl, CURLOPT_POST, true);     //设置为post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        $rtl=curl_exec($curl);
        curl_close($curl);
        return $rtl;
    }
}